package cc.rengu.igas.channel.nucc.core.service.trans;

import cc.rengu.igas.channel.nucc.common.constant.NuccParamConstant;
import cc.rengu.igas.channel.nucc.common.constant.NuccTreeNodeConstant;
import cc.rengu.igas.channel.nucc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.nucc.core.realize.NuccSeqNoManage;
import cc.rengu.igas.channel.nucc.core.realize.NuccService;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccServiceImpl;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccSeqNoManageImpl;
import cc.rengu.igas.channel.nucc.facade.bean.InstgInfBean;
import cc.rengu.igas.channel.nucc.facade.bean.SignInfoBean;
import cc.rengu.igas.channel.nucc.facade.request.RescindSignAcqRequest;
import cc.rengu.igas.channel.nucc.facade.request.RescindSignRequest;
import cc.rengu.igas.channel.nucc.facade.response.RescindSignAcqResponse;
import cc.rengu.igas.channel.nucc.facade.response.RescindSignResponse;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.TimeUtil;

/**
 * 解约
 */
public class EpccRescindSignService extends BizBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        RescindSignRequest request = new RescindSignRequest();
        ConvertUtil.convertOutput(request);

        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.FRONT_SEQ_NUM, request.getHeader().getTraceNo());
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        RescindSignRequest innerRequest = (RescindSignRequest) request;
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ISSR_ID, innerRequest.getInstgId());

        NuccService nuccService = new NuccServiceImpl();
        RescindSignAcqRequest acqRequest = initRescindSignAcqRequest(xmlTreeUtil, innerRequest);
        RescindSignAcqResponse acqResponse = nuccService.rescindSign(acqRequest);

        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(NuccParamConstant.IGAS_SYS_ID);
        if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(acqResponse.getSysRtnCd())) {
            rglog.error("调用网联接口失败，sysRtnCd=[{}],sysRtnDesc=[{}]", acqResponse.getSysRtnCd(), acqResponse.getSysRtnDesc());

            bizResponse.setRespCode(acqResponse.getSysRtnCd());
            bizResponse.setRespDesc(acqResponse.getSysRtnDesc());
        } else {
            bizResponse.setRespCode(acqResponse.getBizStsCd());
            bizResponse.setRespDesc(acqResponse.getBizStsDesc());
        }

        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        RescindSignResponse rescindSignResponse = (RescindSignResponse) response.getResult();
        ConvertUtil.convertInput(rescindSignResponse);
    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }

    private RescindSignAcqRequest initRescindSignAcqRequest(XmlTreeUtil xmlTreeUtil, RescindSignRequest innerRequest) throws Exception {
        RescindSignAcqRequest rescindSignAcqRequest = new RescindSignAcqRequest();
        String instgId = innerRequest.getInstgId();
        SignInfoBean signInfo = new SignInfoBean();
        signInfo.setSgnAcctIssrId(innerRequest.getSgnAcctIssrId());
        signInfo.setSgnAcctTp(innerRequest.getSgnAcctTp());
        signInfo.setSgnNo(innerRequest.getSignNo());
        signInfo.setSgnAcctShrtId(innerRequest.getSgnAcctShrtId());

        InstgInfBean instgInf = new InstgInfBean();
        instgInf.setInstgId(instgId);
        rescindSignAcqRequest.setInstgInf(instgInf);
        rescindSignAcqRequest.setSgnInf(signInfo);

        /*获取网联流水号*/
        NuccSeqNoManage NuccSeqNoManage = new NuccSeqNoManageImpl();
        String epccTxnId = NuccSeqNoManage.genEpccSeqNo();
        rescindSignAcqRequest.setTrxId(epccTxnId);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.CHANNEL_SEQ_NUM, epccTxnId);
        rescindSignAcqRequest.setTrxDtTm(TimeUtil.getDateTime("yyyy-MM-dd'T'HH:mm:ss"));
        /*目前仅支持快捷协议解约，不用填写*/
        rescindSignAcqRequest.setTrxCtgy("");

        return rescindSignAcqRequest;
    }
}
